package com.qiyigu.itc.speech.baidu;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.qiyigu.itc.speech.MyApp;
import com.qiyigu.itc.speech.SpeechService;
import com.qiyigu.itc.speech.baidu.wakeup.WakeupParams;
import com.qiyigu.itc.speech.service.BaseServiceImpl;
import com.qiyigu.itc.speech.baidu.control.MyRecognizer;
import com.qiyigu.itc.speech.baidu.control.MyWakeup;
import com.qiyigu.itc.speech.baidu.recognization.IStatus;
import com.qiyigu.itc.speech.baidu.recognization.StatusRecogListener;
import com.qiyigu.itc.speech.baidu.recognization.offline.OfflineRecogParams;
import com.qiyigu.itc.speech.baidu.wakeup.IWakeupListener;
import com.qiyigu.itc.speech.baidu.wakeup.RecogWakeupListener;
import com.qiyigu.itc.speech.service.listener.OnSpeechListener;

import java.util.Map;

/**
 * 唤醒 加识别
 */
public class BaiduMySpeechServiceImpl extends BaseServiceImpl implements SpeechService, IStatus {

    protected static BaiduMySpeechServiceImpl service = null;
    /**
     * 识别控制器，使用MyRecognizer控制识别的流程
     */
    protected MyRecognizer myRecognizer;

    /**
     * 0: 方案1， 唤醒词说完后，直接接句子，中间没有停顿。
     * >0 : 方案2： 唤醒词说完后，中间有停顿，然后接句子。推荐4个字 1500ms
     * <p>
     * backTrackInMs 最大 15000，即15s
     */
    private int backTrackInMs = 2000;

    protected MyWakeup myWakeup;
    private int status = STATUS_NONE;
    /*
     * Api的参数类，仅仅用于生成调用START的json字符串，本身与SDK的调用无关
     */
    protected Map<String, Object> apiParams;

    protected OnSpeechListener onSpeechListener;

    protected BaiduMySpeechServiceImpl(OnSpeechListener onSpeechListener) {
        service = this;
        this.onSpeechListener = onSpeechListener;
        // 初始化识别引擎
        StatusRecogListener recogListener = new StatusRecogListener(onSpeechListener);
        myRecognizer = new MyRecognizer(MyApp.getInstance(), recogListener);
        //加载离线命令词
        apiParams = OfflineRecogParams.fetchOfflineParams();
        myRecognizer.loadOfflineEngine(apiParams);
        IWakeupListener listener = new RecogWakeupListener(mHandler);
        myWakeup = new MyWakeup(MyApp.getInstance(), listener);
    }

    public static synchronized BaiduMySpeechServiceImpl getInstance(OnSpeechListener
                                                                            onSpeechListener) {
        if (service == null) {
            service = new BaiduMySpeechServiceImpl(onSpeechListener);
        }
        return service;
    }

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == STATUS_WAKEUP_SUCCESS) {
                // 此处 开始正常识别流程
              /*  Map<String, Object> params = new LinkedHashMap<String, Object>();
                params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);
                params.put(SpeechConstant.VAD, SpeechConstant.VAD_DNN);
                int pid = PidBuilder.create().model(PidBuilder.INPUT).toPId();
                //如识别短句，不需要需要逗号，将PidBuilder.INPUT改为搜索模型PidBuilder.SEARCH
                params.put(SpeechConstant.PID, pid);
                if (backTrackInMs > 0) { // 方案1， 唤醒词说完后，直接接句子，中间没有停顿。
                    params.put(SpeechConstant.AUDIO_MILLS, System.currentTimeMillis() -
                            backTrackInMs);

                }*/
                myRecognizer.cancel();
                myRecognizer.start(apiParams);
                if (onSpeechListener != null) {
                    onSpeechListener.onWarkUp();
                }
            }

        }
    };

    @Override
    public synchronized void startSpeech() {
        if (status != STATUS_NONE) {
            return;
        }
//        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ActivityRecog.this);
//        Map<String, Object> params = apiParams.fetch(sp);
        WakeupParams wakeupParams = new WakeupParams(MyApp.getInstance());
        Map<String, Object> params = wakeupParams.fetch();
        myWakeup.start(params);
        status = STATUS_WAITING_READY;
        logger.i("启动唤醒");
    }

    @Override
    public synchronized void stopSpeech() {
        myWakeup.stop();
        myRecognizer.stop();
        status = STATUS_NONE;
    }

    @Override
    public synchronized void releaseSpeech() {
        myWakeup.release();
        myRecognizer.release();
        status = STATUS_NONE;
    }
}
